#include "os_interface.h"

#include <iostream>
#include <fstream>

#include <pthread.h>
#include <semaphore.h>
#include <sys/mman.h>
#include <sys/types.h>
#include <sys/socket.h>

using namespace std;

/*
 * 系统调用相关接口实现
 */

int os_task_create(os_task_attr_t* attr) {
  pthread_t task;
  if (pthread_create(&task, NULL, (void* (*)(void*))attr->func, attr->arg) != 0) {
    return -1;
  }
  pthread_detach(task);
  return 0;
}

void os_task_destroy(int task_id) {
  // Not implemented yet
}

int os_read_device(int device_id, void* buffer, uint32_t size) {
      int fd = open("/dev/" + to_string(device_id), O_RDONLY);
    if (fd == -1) {
    return -1;
    }
    int nread = read(fd, buffer, size);
    close(fd);
    return nread;
  }

  int os_write_device(int device_id, const void* buffer, uint32_t size) {
    int fd = open("/dev/" + to_string(device_id), O_WRONLY);
    if (fd == -1) {
    return -1;
    }
    int nwrite = write(fd, buffer, size);
    close(fd);
    return nwrite;
  }

  int os_lock_resource(int resource_id) {
    sem_t* sem = sem_open("/resource_" + to_string(resource_id), O_CREAT, 0666, 1);
    if (sem == SEM_FAILED) {
    return -1;
    }
    return sem_wait(sem);
  }

  int os_unlock_resource(int resource_id) {
    sem_t* sem = sem_open("/resource_" + to_string(resource_id), 0);
    if (sem == SEM_FAILED) {
    return -1;
    }
    return sem_post(sem);
  }

  // 其他系统调用相关接口的实现

  /*

  硬件驱动相关接口实现
  */
  int os_driver_open(int driver_id) {
    int fd = open("/dev/" + to_string(driver_id), O_RDWR);
    if (fd == -1) {
    return -1;
    }
    return fd;
  }

  void os_driver_close(int driver_id) {
  // Not implemented yet
  }

  int os_driver_read(int driver_id, void* buffer, uint32_t size) {
    int fd = os_driver_open(driver_id);
    if (fd == -1) {
    return -1;
    }
    int nread = read(fd, buffer,size);
  close(fd);
  return nread;
}

int os_driver_write(int driver_id, const void* buffer, uint32_t size) {
  int fd = os_driver_open(driver_id);
  if (fd == -1) {
  return -1;
  }
  int nwrite = write(fd, buffer, size);
  close(fd);
  return nwrite;
}

int os_control_device(int device_id, uint32_t cmd, void* data) {
  int fd = os_driver_open(device_id);
  if (fd == -1) {
  return -1;
  }
  int result = ioctl(fd, cmd, data);
  close(fd);
  return result;
}

// 其他硬件驱动相关接口的实现

/*

任务管理相关接口实现
*/
void os_scheduler_start(void) {
// Not implemented yet
}

void os_scheduler_stop(void) {
// Not implemented yet
}

void os_scheduler_restart(void) {
// Not implemented yet
}

void os_enter_critical(void) {
pthread_mutex_lock(&critical_mutex);
}

void os_exit_critical(void) {
pthread_mutex_unlock(&critical_mutex);
}

// 其他任务管理相关接口的实现

/*

信号管理相关接口实现
*/
int os_wait_signal(int signal_id) {
// Not implemented yet
return 0;
}

int os_send_signal(int signal_id) {
// Not implemented yet
return 0;
}

// 其他信号管理相关接口的实现

/*

内存分配相关接口实现
*/
void* os_malloc(size_t size) {
void* ptr = mmap(NULL, size, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
if (ptr == MAP_FAILED) {
return NULL;
}
return ptr;
}

void os_free(void* ptr) {
munmap(ptr, sizeof(ptr));
}

// 其他内存分配相关接口的实现

// 临界区互斥量
static pthread_mutex_t critical_mutex = PTHREAD_MUTEX_INITIALIZER;